﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using FreedomX2.Core.Encrypt;

namespace FreedomX2.Core.IO
{
    public static class SerializeEx
    {
        public static string EsSerializable(this DataSet ds)
        {
            string AllString = "";//返回的字符串
            string TableName = "";//表名称
            string TableColumns = "";//表结构
            string TableData = "";//表数据
            int TablesNum = ds.Tables.Count;
            for (int i = 0; i < TablesNum; i++)
            {
                TableName += ds.Tables[i].TableName.Replace(",", "&douhao&").Replace(";", "&fenhao&").Replace("#", "&jinghao&").Replace("?", "&wenhao&").Replace(":", "&maohao&") + ",";
                for (int j = 0; j < ds.Tables[i].Columns.Count; j++)
                {
                    TableColumns += ds.Tables[i].Columns[j].ToString().Replace(",", "&douhao&").Replace(";", "&fenhao&").Replace("#", "&jinghao&").Replace("?", "&wenhao&").Replace(":", "&maohao&") + ",";
                }

                TableColumns = TableColumns.Substring(0, TableColumns.LastIndexOf(",")) + ";";
                for (int k = 0; k < ds.Tables[i].Rows.Count; k++)//行
                {
                    for (int z = 0; z < ds.Tables[i].Columns.Count; z++)//列
                    {
                        TableData += ds.Tables[i].Rows[k][z].ToString().Replace(",", "&douhao&").Replace(";", "&fenhao&").Replace("#", "&jinghao&").Replace("?", "&wenhao&").Replace(":", "&maohao&") + ",";
                    }
                    TableData = TableData.Substring(0, TableData.LastIndexOf(",")) + "#";
                }
                TableData = TableData.Substring(0, TableData.LastIndexOf("#")) + ";";
            }
            TableName = TableName.Substring(0, TableName.LastIndexOf(",")) + "?";
            TableColumns = TableColumns.Substring(0, TableColumns.LastIndexOf(";")) + "?";
            TableData = TableData.Substring(0, TableData.LastIndexOf(";"));
            AllString = TableName + TableColumns + TableData;
            AllString = AllString.Encrypt();
            return AllString;
        }

        public static string EsSerializable(this IDataReader idr)
        {
            string TableData = "";//表数据
            string TableColumns = "";//表结构
            DataTable TableName = idr.GetSchemaTable();
            for (int i = 0; i < TableName.Rows.Count; i++)
            {
                TableColumns += TableName.Rows[i]["ColumnName"].ToString() + ",";//获得DataReader表结构
            }
            TableColumns = TableColumns.Substring(0, TableColumns.LastIndexOf(",")) + ";";
            while (idr.Read())//读取表数据
            {
                for (int i = 0; i < TableName.Rows.Count; i++)
                {
                    TableData += idr[TableName.Rows[i]["ColumnName"].ToString()].ToString() + ",";//将表结构和表数据拼接成串
                }
                TableData = TableData.Substring(0, TableData.LastIndexOf(",")) + ":";
            }
            TableData = TableData.Substring(0, TableData.LastIndexOf(":"));
            idr.Close();
            string AllString = TableColumns + TableData;
            AllString = AllString.Encrypt();
            return AllString;
        }
        public enum ReturnClass
        {
            ReturnDataTable,
            ReturnDataSet
        }
        public static T EsDeserializable<T>(this string strdata) where T : class
        {
            if (typeof(T) == typeof(DataTable))
            {
                return EsDeserializable2(strdata) as T;
            }
            else if (typeof(T) == typeof(DataSet))
            {
                return EsDeserializable1(strdata) as T;
            }
            else
            {
                return default(T);
            }
        }
        private static DataSet EsDeserializable1(this string DataString)
        {
            string DecodeDataString = DataString.Decrypt();
            DataSet ds = new DataSet();
            string[] aryStr = DecodeDataString.Split('?');//截取表名0，字段名1，表数据2
            string[] TableName = aryStr[0].Split(',');//截取单个表名
            string[] TableColumnsList = aryStr[1].Split(';');//截取单个表，表字段
            string[] TableDataList = aryStr[2].Split(';');//截取单个表，表数据
            string[] TableColumns = null;
            string[] TableData = null;
            string[] Data = null;
            for (int i = 0; i < TableName.Length; i++)//循环插入表名称
            {
                ds.Tables.Add(TableName[i].Replace("&douhao&", ",").Replace("&fenhao&", ";").Replace("&jinghao&", "#").Replace("&wenhao&", "?").Replace("&maohao&", ":"));//ds中添加表
                TableColumns = TableColumnsList[i].Split(',');
                TableData = TableDataList[i].Split('#');
                for (int j = 0; j < TableColumns.Length; j++)//循环插入表字段
                {
                    ds.Tables[TableName[i]].Columns.Add(TableColumns[j].Replace("&douhao&", ",").Replace("&fenhao&", ";").Replace("&jinghao&", "#").Replace("&wenhao&", "?").Replace("&maohao&", ":"));//ds添加的表中添加列
                }
            }
            for (int i = 0; i < TableName.Length; i++)//循环表名称
            {
                TableData = TableDataList[i].Split('#');
                for (int j = 0; j < TableData.Length; j++)//循环表字段
                {
                    Data = TableData[j].Split(',');
                    for (int k = 0; k < Data.Length; k++)//循环表内数据
                    {
                        Data[k] = Data[k].Replace("&douhao&", ",").Replace("&fenhao&", ";").Replace("&jinghao&", "#").Replace("&wenhao&", "?").Replace("&maohao&", ":");
                    }
                    ds.Tables[TableName[i]].Rows.Add(Data);
                }
            }
            return ds;
        }
        private static DataTable EsDeserializable2(this string DataString)
        {
            //id,name;1,aaa:2,b:3,cc:4,dddd
            string DecodeDataString = DataString.Decrypt();
            DataTable dt = new DataTable();
            string[] aryStr = DecodeDataString.Split(';');
            string[] TableColumns = aryStr[0].Split(',');
            string[] TableDataList = aryStr[1].Split(':');
            string[] TableData = null;
            for (int i = 0; i < TableColumns.Length; i++)
            {
                dt.Columns.Add(TableColumns[i]);
            }
            for (int i = 0; i < TableDataList.Length; i++)
            {
                TableData = TableDataList[i].Split(',');
                dt.Rows.Add(TableData);
            }
            return dt;
        }



    }
}
